package com.ruoyi.framework.utils;


import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.constant.SmsConst;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.exception.user.CaptchaException;
import com.ruoyi.common.exception.user.CaptchaExpireException;
import com.ruoyi.common.utils.MessageUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.framework.manager.AsyncManager;
import com.ruoyi.framework.manager.factory.AsyncFactory;
import org.dromara.sms4j.api.SmsBlend;
import org.dromara.sms4j.api.entity.SmsResponse;
import org.dromara.sms4j.core.factory.SmsFactory;

public class SmsUtil {
    public static String send(String phone,String sentContent){

        SmsResponse smsResponse = SmsFactory.getSmsBlend("config1").sendMessage(phone,sentContent);
        return "";
    }




    public static void checkCode(String phoneNumber,String code,String uuid){
        RedisCache redisCache= SpringUtils.getBean(RedisCache.class);
        // 校验验证码
        String verifyKey = SmsConst.REDIS_KEY_SMS_LOGIN + uuid;
        String captcha = redisCache.getCacheObject(verifyKey);
        if(captcha == null) {
            AsyncManager.me().execute(AsyncFactory.recordLogininfor(phoneNumber, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.expire")));
            // 抛出一个验证码过期异常
            throw new CaptchaExpireException();
        }
        if(!captcha.equals(code.trim())){
            AsyncManager.me().execute(AsyncFactory.recordLogininfor(phoneNumber, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.error")));
            // 抛出一个验证码错误的异常
            throw new CaptchaException();
        }
        redisCache.deleteObject(verifyKey);
    }
}
